package com.kotlin.user.ui.activity

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import com.kotlin.base.ui.activity.BaseMvpActivity
import com.kotlin.user.R
import com.kotlin.user.presenter.RegisterPresenter
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseMvpActivity<RegisterPresenter>() ,SurfaceHolder.Callback{
    
    var isSurfaceCreated = false
    var left =0
    var top =0
    var right =0
    var bootom =0
    var width =0
    var height =0
    var surfaceView:SurfaceView? = null

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun surfaceCreated(holder: SurfaceHolder?) {
        isSurfaceCreated = true;
        left = surfaceView!!.getLeft();
        top = surfaceView!!.getTop();
        right = surfaceView!!.getRight();
        bootom = surfaceView!!.getBottom();
        width = surfaceView!!.getWidth();
        height = surfaceView!!.getHeight();
        Log.d("---","left = "+left+" top = "+top+" right = "+right+" bootom = "+bootom);
        Log.d("====","width = "+width+" height = "+height);
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    var holder:SurfaceHolder? = null
    

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        surfaceView = mSurfaceView as SurfaceView
        holder = surfaceView!!.holder
        holder!!.addCallback(this)

        findViewById<View>(R.id.mBtn).setOnClickListener { MyThread().start() }


    }

    override fun initComponentInject() {

    }


 inner class MyThread : Thread() {
    private var isDrawing = true
    private val paint = Paint()
    internal var rectF = RectF(20f, 20f, (right - left - 20).toFloat(), (bootom - top - 20).toFloat())
    internal var num = 360
    internal var current = 0
    override fun run() {
        Log.d("----", "run")
        super.run()
        var canvas: Canvas? = null
        while (isDrawing) {
            Log.d("----", "isDrawing")
            canvas = holder!!.lockCanvas()//
            canvas!!.drawColor(Color.WHITE)
            paint.setAntiAlias(true)
            paint.setStrokeWidth(12.0f)
            paint.setStyle(Paint.Style.STROKE)
            paint.setColor(Color.rgb(0xe9, 0xe9, 0xe9))

            canvas!!.drawArc(rectF, (-90).toFloat(), 360F, false, paint)//-90表示从竖直上的方向开始顺时针绘制 第三个参数false 画弧度的时候 边框不显示

            paint.setColor(Color.rgb(0xf8, 0x60, 0x30))
            canvas!!.drawArc(rectF, (-90).toFloat(), current.toFloat(), false, paint)//

            paint.setStyle(Paint.Style.FILL)
            paint.setStrokeWidth(1.0f)
            paint.setTextSize(50F)
            paint.setColor(Color.BLUE)
            paint.setTextAlign(Paint.Align.CENTER)
            val baseX = width / 2
            val baseY = height / 2
            canvas!!.drawText((current * 100 / 360).toString() + "%", baseX.toFloat(), baseY.toFloat(), paint)
            try {
                if (current >= 360) {
                    isDrawing = false
                    current = 360
                } else {
                    current = current + 10
                }
                Thread.sleep(500)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }

            holder!!.unlockCanvasAndPost(canvas)
        }
        isDrawing = true
    }
}

}
